package dk.aau.medialogy.y2013.p4;

import ddf.minim.AudioOutput;
import ddf.minim.ugens.Oscil;
import ddf.minim.ugens.UGen;
import ddf.minim.ugens.Waveform;
import ddf.minim.ugens.Waves;

public class Oscillator {
	
	//Public variables
	
	//Private variables - pretty self explanatory
	float frequency; 
	float amplitude;
	Waveform type;
	AudioOutput out;
	
	//Declare an Oscil UGen
	Oscil osc;

	//Constructor
	public Oscillator(float frequency, float amplitude, AudioOutput output, String type)
	{
		this.frequency = frequency;
		this.amplitude = amplitude;
		out = output;
		switch (type)
		{
			case "saw":
				osc = new Oscil(this.frequency, this.amplitude, Waves.saw(15));
				break;
		
			case "sine":
				osc = new Oscil(this.frequency, this.amplitude, Waves.SINE);
				break;
		}
		//used in the full program but in the showcase used during tests we did not want a working oscillator
		//start();
	}
	
	//Start the oscillator / restart after changes
	public void start()
	{
		osc.patch(out);
	}
	
	public void patch(UGen output)
	{
		osc.patch(output);
	}
	
	//Stop the oscillator
	public void stop()
	{
		osc.unpatch(out);
	}
	
	//Unpatch but also make sure that if it is not patched to the given output the program just ignores it
	public void unpatch(UGen output)
	{
		try {
			osc.unpatch(output);
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void changeFrequency(float frequency)
	{
		//stop();
		this.frequency = frequency;
		osc.setFrequency(this.frequency);
		//start();
	}
	
	public float getFrequency() {
		return frequency;
	}

	public float getAmplitude() {
		return amplitude;
	}
	
	public float getValue()
	{
		return osc.getLastValues()[0];
	}


}
